package com.huidz.calendar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.transition.Slide;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.huidz.calendar.R;
import com.huidz.calendar.activityadapter.XianDaiWenAdapter;
import com.huidz.calendar.util.Apiutils;

import java.util.ArrayList;
import java.util.List;

public class HuangliXdwActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView home_list;
    private List<Fragment> fraglist;
    private ImageView comeback;
    private ArrayList<String> new_list_view;
    private RelativeLayout yiji_rela, chongsha_rela, zhishen_rela, wuxing_rela, jishen_rela, xiongshen_rela, taishen_rela, pengzu_rela, xingsu_rela;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.xiandaiwen_listhead);
        inti();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        Intent huangli = getIntent();
        String types = huangli.getStringExtra("type");
        yiji_rela = findViewById(R.id.yiji_rela);
        chongsha_rela = findViewById(R.id.chongsha_rela);
        zhishen_rela = findViewById(R.id.zhishen_rela);
        wuxing_rela = findViewById(R.id.wuxing_rela);
        jishen_rela = findViewById(R.id.jishen_rela);
        xiongshen_rela = findViewById(R.id.xiongshen_rela);
        taishen_rela = findViewById(R.id.taishen_rela);
        pengzu_rela = findViewById(R.id.pengzu_rela);
        xingsu_rela = findViewById(R.id.xingsu_rela);
        yiji_rela.setOnClickListener(this);
        chongsha_rela.setOnClickListener(this);
        zhishen_rela.setOnClickListener(this);
        wuxing_rela.setOnClickListener(this);
        jishen_rela.setOnClickListener(this);
        xiongshen_rela.setOnClickListener(this);
        taishen_rela.setOnClickListener(this);
        pengzu_rela.setOnClickListener(this);
        xingsu_rela.setOnClickListener(this);
        /*********************/
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        home_list = findViewById(R.id.hlxdw_list);
        new_list_view = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            new_list_view.add("" + i);
        }
        XianDaiWenAdapter myAdapter = new XianDaiWenAdapter(new_list_view,
                HuangliXdwActivity.this);
        home_list.setAdapter(myAdapter);
//        home_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Apiutils.SetToast(HuangliXdwActivity.this, "点击了:" + i);
//            }
//        });
        home_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                switch (i) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: // 没有滚动的时候
                        if (home_list.getLastVisiblePosition() == (home_list
                                .getCount() - 1)) {
                            ChangeColor(home_list
                                    .getCount() - 1);
                        }

                        if (home_list.getFirstVisiblePosition() == 0) {
                            ChangeColor(home_list.getFirstVisiblePosition());
                        }

                        break;

                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        ChangeColor(home_list.getLastVisiblePosition());
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            }
        });

        if (types.equals("1")) {
            String listloca = huangli.getStringExtra("listloca");
            if (listloca.equals("yi")) {
                ChangeColor(0);
                home_list.setSelection(0);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPositionFromTop(0, 0, 0);
//                    }
//                });

            } else if (listloca.equals("ji")) {
                ChangeColor(0);
                home_list.setSelection(0);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPositionFromTop(1, -100, 500);
//                    }
//                });
            } else if (listloca.equals("pz")) {
                ChangeColor(7);
                home_list.setSelection(7);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(7);
//                    }
//                });
            } else if (listloca.equals("zhishen")) {
                ChangeColor(2);
                home_list.setSelection(2);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(2);
//                    }
//                });
            } else if (listloca.equals("xingxiu")) {
                ChangeColor(8);
                home_list.setSelection(8);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(8);
//                    }
//                });
            } else if (listloca.equals("chongsha")) {
                ChangeColor(1);
                home_list.setSelection(1);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(1);
//                    }
//                });
            } else if (listloca.equals("xiongshen")) {
                ChangeColor(5);
                home_list.setSelection(5);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(5);
//                    }
//                });
            } else if (listloca.equals("wuxing")) {
                ChangeColor(3);
                home_list.setSelection(3);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(3);
//                    }
//                });
            } else if (listloca.equals("taishen")) {
                ChangeColor(6);
                home_list.setSelection(6);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(6);
//                    }
//                });
            } else if (listloca.equals("jishen")) {
                ChangeColor(4);
                home_list.setSelection(4);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(4);
//                    }
//                });
            }
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;
            case R.id.yiji_rela:
                ChangeColor(0);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
                        home_list.smoothScrollToPosition(0);
                    }
                });
                break;
            case R.id.chongsha_rela:
                ChangeColor(1);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
//                        home_list.smoothScrollToPosition(1);
                        home_list.smoothScrollToPositionFromTop(1, 0, 60);
                    }
                });
                break;
            case R.id.zhishen_rela:
                ChangeColor(2);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
//                        home_list.smoothScrollToPosition(2);
                        home_list.smoothScrollToPositionFromTop(2, 0, 60);
                    }
                });
                break;
            case R.id.wuxing_rela:
                ChangeColor(3);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
//                        home_list.smoothScrollToPosition(3);
                        home_list.smoothScrollToPositionFromTop(3, 0, 60);
                    }
                });
                break;
            case R.id.jishen_rela:
                ChangeColor(4);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
//                        home_list.smoothScrollToPosition(4);
                        home_list.smoothScrollToPositionFromTop(4, 0, 60);
                    }
                });
                break;
            case R.id.xiongshen_rela:
                ChangeColor(5);
                home_list.post(new Runnable() {
                    @Override
                    public void run() {
//                        home_list.smoothScrollToPosition(5);
                        home_list.smoothScrollToPositionFromTop(5, 0, 60);
                    }
                });
                break;
            case R.id.taishen_rela:
                ChangeColor(6);
                home_list.setSelection(6);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_list.smoothScrollToPosition(6);
//                        home_list.smoothScrollToPositionFromTop(6, 0, 60);
//                    }
//                });
                break;
            case R.id.pengzu_rela:
                ChangeColor(7);
                home_list.setSelection(7);
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
////                        home_list.smoothScrollToPosition(7);
//                        home_list.smoothScrollToPositionFromTop(7, 0, 60);
//                    }
//                });
                break;
            case R.id.xingsu_rela:
                ChangeColor(8);
                home_list.setSelection(8);
//                home_list.performItemClick(null, 8, 0);
                home_list.performItemClick(home_list.getChildAt(8), 8, home_list.getItemIdAtPosition(8));
//                home_list.post(new Runnable() {
//                    @Override
//                    public void run() {
////                        home_list.smoothScrollToPosition(8);
////                        home_list.smoothScrollToPositionFromTop(8, 0, 60);
//                    }
//                });
                break;
        }
    }

    public void ChangeColor(int pos) {
        switch (pos) {
            case 0:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 1:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 2:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 3:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 4:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 5:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 6:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 7:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                break;
            case 8:
                yiji_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                chongsha_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                zhishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                wuxing_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                jishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xiongshen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                taishen_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                pengzu_rela.setBackgroundResource(R.drawable.hl_list_big_50);
                xingsu_rela.setBackgroundResource(R.drawable.hl_list_big);
                break;
        }

    }

    public static long firstTime = 0;


}
